import { NodePoint } from "@/utils/antvx6/nodePoint";
import { getNodeMaxId } from "@/utils/antvx6/nodeOrEdgeMaxId";
/**
 * 添加节点
 * @param { HTMLElement }  elementGraph -- antv x6 元素
 * @param { number } _width 可添加范围内画布的宽
 * @param { number } _height 可添加范围内画布的高
 */
export const addNodes = async (elementGraph: any, _width: number, _height: number): Promise<void> => {
  elementGraph.on("blank:click", async ({ x, y }) => {
    const xSum: number = Math.abs(x);
    const ySum: number = Math.abs(y);
    const widthGraph: number = Math.ceil(_width);
    const heightGraph: number = Math.ceil(_height);
    if (xSum > widthGraph || ySum > heightGraph || x < 0 || y < 0)
      return ElMessage.error(
        `您添加的画布节点【x坐标: ${x}】【y坐标: ${y}】已超出范围，规定范围是：${_width} * ${_height}内`
      );
    // 获取画布中唯一的节点ID
    const nodeSum: number = getNodeMaxId(elementGraph);
    const ids: string = `n${nodeSum}`;
    const nodeP = new NodePoint();
    const param = {
      nodeNameEn: `n${nodeSum}`,
      nodeNameCn: `n${nodeSum}`,
      hasDirection: 0,
      mapNodeType: 9,
      x: x,
      y: y,
    };
    nodeP.setData({
      id: ids,
      x: x,
      y: y,
      data: {
        x: x,
        y: y,
        hasDirection: param.hasDirection,
        mapNodeType: param.mapNodeType,
        nodeNameCn: param.nodeNameCn,
        nodeNameEn: param.nodeNameEn,
        angle: 0,
      },
    });
    elementGraph.addNode(nodeP.getModel());
  });
};


